//
//  ViewController.m
//  traverse
//
//  Created by wanglei on 17/2/13.
//  Copyright © 2017年 wanglei. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self enumerateDemo4];
}

- (void)NSEnumeratorDemo1{
    
    NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"O", @"S"];
    
   // NSEnumerator *enumerator = [iosArray objectEnumerator];//正向遍历
    
    NSEnumerator *enumerator = [iosArray reverseObjectEnumerator];//反向遍历
    
    id object;
    
    while ((object = [enumerator nextObject]) != nil) {
        
        //处理枚举器中的数据
        NSLog(@"%@", object);
}

}

- (void)enumerateDemo2{
    
    NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"O", @"S"];
    
    [iosArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        NSLog(@"idx = %lu,obj = %@",(unsigned long)idx, obj);
        
        if ([obj isEqualToString:@"O"]) {
            *stop = YES;
        }
    }];
}

- (void)enumerateDemo3{
    
    NSDictionary *dict = @{@"A":@"11", @"B":@"22", @"C":@"33"};
    
    [dict enumerateKeysAndObjectsUsingBlock:^( NSString *key, NSString *obj, BOOL * _Nonnull stop) {
        
        NSLog(@"key = %@,obj = %@",key,obj);
        
        if ([obj isEqualToString:@"22"]) {
            
            *stop = YES;
        }
    }];
    
}

- (void)enumerateDemo4{
    
    NSArray *array = @[@"A",@"B",@"C",@"D",@"F",@"G",@"H"];
    
    [array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        // 没有开启额外的线程
        NSLog(@"obj = %@,idx = %lu,thread = %@",obj,(unsigned long)idx,[NSThread currentThread]);
        
        if (idx == 4) {
            *stop = YES;
        }
        
    }];
    
}

- (void)enumerateDemo5{
    
    NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"O", @"S"];
    
    NSMutableArray *iosMutableArray = [NSMutableArray arrayWithArray:iosArray];
    
    [iosMutableArray enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSString *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        obj = [NSString stringWithFormat:@"_%@", obj];
        
        [iosMutableArray replaceObjectAtIndex:idx withObject:obj];
        
        NSLog(@"thread = %@,obj = %@",[NSThread currentThread],obj);
        
        if ([obj isEqualToString:@"_V"]) {
            
            *stop = YES;
        }
    }];
    
    NSLog(@"iosMutableArray = %@",iosMutableArray);
    
}
- (void)forinDemo1{
    
    NSDictionary *dict = @{@"1":@"11", @"2":@"22", @"3":@"33"};
    
    for (NSString *key in dict) {
        
        //处理字典的键值
        
        NSString *value = dict[key];
        
        NSLog(@"key = %@,value = %@",key,value);
    }
}

- (void)forinDemo2{
    
    NSArray *iosArray = @[@"L", @"O", @"V", @"E", @"I", @"O", @"S"];
    
    for (NSString *obj in iosArray) {
        
        //处理数组中的数据
        NSLog(@"%@", obj);
    }
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
